home *** CD-ROM | disk | FTP | other *** search
- /* MBTOENDPTS: function converts a parameterized line consisting of
- * slope (m), y-intercept (b), and two endpoints of data
- * into a line fit by the parameters, but bounded by the
- * bounding box of the endpoints
- * usage: mbtoendpts (end1, end2, m, b, width, height,
- */
-
- #define ABS(A) (((A) >= 0.0) ? (A): -(A)) /* integer absolute value */
-
- #include <images.h>
-
- int
- mbtoendpts (end1, end2, m, b, width, height, pt1, pt2)
- struct point end1, end2; /* endpoints of data */
- double m, b; /* slope and y-intercept parameters of fit */
- long width, height; /* image size */
- struct point *pt1, *pt2; /* endpoints of line fit */
-
- {
- struct dpoint min, max; /* floating point point */
-
- /* determine bounding box */
- min.x = (end1.x < end2.x) ? (double) end1.x : (double) end2.x;
- min.y = (end1.y < end2.y) ? (double) end1.y : (double) end2.y;
- max.x = (end1.x > end2.x) ? (double) end1.x : (double) end2.x;
- max.y = (end1.y > end2.y) ? (double) end1.y : (double) end2.y;
-
- /* endpoints for zero slope */
- if (m == 0.0) {
- pt1->x = (long) (min.x + 0.5);
- pt1->y = (long) (min.y + 0.5);
- pt2->x = (long) (max.x + 0.5);
- pt2->y = (long) (max.y + 0.5);
- return (0);
- }
-
- /* endpoints are upper and lower bounds of bounding box, depending on slope */
- if (ABS (m) < 1.0) { /* use x bounds for low slope */
- pt1->x = (long) min.x;
- pt1->y = (long) (m * min.x + b + 0.5);
- if (pt1->y < 0) {
- pt1->y = 0;
- pt1->x = (long) ((-b) / m + 0.5);
- }
- else if (pt1->y >= height) {
- pt1->y = height - 1;
- pt1->x = (long) ((pt1->y - b) / m + 0.5);
- }
- pt2->x = (long) max.x;
- pt2->y = (long) (m * max.x + b + 0.5);
- if (pt2->y < 0) {
- pt2->y = 0;
- pt2->x = (long) ((-b) / m + 0.5);
- }
- else if (pt2->y >= height) {
- pt2->y = height - 1;
- pt2->x = (long) ((pt2->y - b) / m + 0.5);
- }
- }
- else {
- pt1->y = (long) min.y;
- pt1->x = (long) ((min.y - b) / m + 0.5);
- if (pt1->x < 0) {
- pt1->x = 0;
- pt1->y = (long) (b + 0.5);
- }
- else if (pt1->x >= width) {
- pt1->x = width - 1;
- pt1->y = (long) (m * pt1->x + b + 0.5);
- }
- pt2->y = (long) max.y;
- pt2->x = (long) ((max.y - b) / m + 0.5);
- if (pt2->x < 0) {
- pt2->x = 0;
- pt2->y = (long) (b + 0.5);
- }
- else if (pt2->x >= width) {
- pt2->x = width - 1;
- pt2->y = (long) (m * pt2->x + b + 0.5);
- }
- }
-
- return (0);
- }
-